package com.stm.bi.report.productTrend;

import com.stm.bi.core.util.DateUtils;
import com.stm.bi.core.util.SqlUtils;
import com.stm.bi.dto.query.*;
import com.stm.bi.enums.DateType;
import com.stm.bi.enums.ParamType;
import com.stm.bi.model.Chart;
import com.stm.bi.model.DatatablePage;
import com.stm.bi.model.Title;
import com.stm.framework.core.util.StringUtil;
import com.stm.framework.dbcp.JdbcService;
import com.stm.framework.dto.Response;
import com.stm.saas.api.ProductStyleRemoteService;
import com.stm.saas.dto.query.ProductStylePageQry;
import com.stm.saas.dto.vo.ProductStyleQueryVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

import static com.stm.bi.constant.IndexConstants.DIGESTIBILITY_INDEX_NAME;
import static com.stm.bi.constant.IndexConstants.POSITIVE_SALES_VOLUME_FIELD_NAME;
import static com.stm.bi.constant.IndexConstants.SPECIAL_SALES_VOLUME_FIELD_NAME;
import static com.stm.bi.constant.IndexConstants.*;
import static com.stm.bi.constant.PlanConstants.TYPE_WEEK;
import static com.stm.bi.enums.GroupType.SUM;
import static com.stm.saas.constant.IndexConstants.POSITIVE_SALES_VOLUME_INDEX_NAME;
import static com.stm.saas.constant.IndexConstants.SPECIAL_SALES_VOLUME_INDEX_NAME;
import static com.stm.saas.constant.IndexConstants.*;

@Component
public class StyleThreeSevenReportQueryExe {
    
    @Resource
    private JdbcService jdbcService;
    @Resource
    private ProductStyleRemoteService productStyleRemoteService;
    @Resource
    private ProductDataQueryExe productDataQueryExe;
    
    public Chart<List<List<Object>>> threeSevenChart(ChartReportQry reportQry) {
        String seasonParam = reportQry.getParams().stream().filter(item -> ParamType.SEASON.getValue().equals(item.getType())).findFirst().map(ParamQry::getCode).orElse(null);
        // 款式过滤
        List<String> codes = new ArrayList<>();
        if (StringUtil.isNotEmpty(seasonParam)) {
            Response<List<ProductStyleQueryVO>> res = productStyleRemoteService.selectList(new ProductStylePageQry().setSeason(seasonParam));
            if (!res.isSuccess()) {
                return new Chart<>();
            }
            codes.addAll(res.get().stream().map(ProductStyleQueryVO::getCode).toList());
        }
        
        Chart<List<List<Object>>> chart = new Chart<>();
        List<List<Object>> datas = new ArrayList<>();

        chart.getItems().add(POSITIVE_SALES_VOLUME_INDEX_NAME);
        chart.getItems().add(SPECIAL_SALES_VOLUME_INDEX_NAME);
        chart.getItems().add("库存");
        chart.getItems().add("售罄率");

        // 售罄率 = 销量 / (最后一个月的期末库存 + 销量)
        // 累计销售占比 = 单款销量/总销量 （每个款式顺序累加）
        List<GroupField> groupFields = new ArrayList<>(Arrays.asList(
//                new GroupField().setFieldName(QTY_FIELD_NAME).setGroupType(GroupType.SUM.getValue()),
                new GroupField().setFieldName(POSITIVE_SALES_VOLUME_FIELD_NAME).setGroupType(SUM.getValue()),
                new GroupField().setFieldName(SPECIAL_SALES_VOLUME_FIELD_NAME).setGroupType(SUM.getValue())
        ));

        // 合计查询
        DimensionDataGroupQry qry = new DimensionDataGroupQry()
                .setSalesType(reportQry.getSalesType())
                .setDeptId(reportQry.getDeptId())
                .setDateType(reportQry.getDateType().getValue())
                .setStartDate(reportQry.getStartDate())
                .setEndDate(reportQry.getEndDate())
                .setGroupFields(groupFields)
                .setGroupBy(Arrays.asList(STYLE_CODE_FIELD, STYLE_NAME_FIELD));
        if (!codes.isEmpty()) {
            Map<String, Object> other = new HashMap<>();
            other.put(STYLE_CODE_FIELD, codes);
            qry.setOtherConditions(other);
        }


        // 最后时间节点的期末库存
        String s = productDataQueryExe.selectMaxInventoryDate(reportQry.getStartDate(), reportQry.getEndDate(), reportQry.getDateType().getValue());

        List<Map> dataList;
        List<Map> dataEndMonth;
        if (StringUtil.isNotEmpty(seasonParam) && codes.isEmpty()) {
            dataList = new ArrayList<>();
            dataEndMonth = new ArrayList<>();
        } else {
            String sql = SqlUtils.buildSql(qry, "ads_base_product_multi_sales");
            dataList = jdbcService.selectList(sql, new Object[]{});

            if(TYPE_WEEK.equals(reportQry.getDateType().getValue())){
                qry.setStartDate(DateUtils.getWeek(s))
                        .setEndDate(DateUtils.getWeek(s));
            }else {
                qry.setStartDate(s)
                        .setEndDate(s);
            }
            dataEndMonth = productDataQueryExe.selectInventoryData(qry, STYLE_CODE_FIELD, new ArrayList<>());
        }

        // 重复编码处理特殊处理
        Map<String, Map> dataEndMonthMap = new HashMap<>();
        for (Map map : dataEndMonth) {
            String busId = (String) map.get(STYLE_CODE_FIELD);
            if (!dataEndMonthMap.containsKey(busId)) {
                dataEndMonthMap.put(busId, map);
            } else {
                Map oldData = dataEndMonthMap.get(busId);
                oldData.put(INVENTORY_FIELD, (oldData.get(INVENTORY_FIELD) == null ? BigDecimal.ZERO : (BigDecimal) oldData.get(INVENTORY_FIELD))
                        .add(map.get(INVENTORY_FIELD) == null ? BigDecimal.ZERO : (BigDecimal) map.get(INVENTORY_FIELD)));
            }
        }
        dataEndMonth = new ArrayList<>(dataEndMonthMap.values());

        Map<String, BigDecimal> endInventoryMap = dataEndMonth.stream()
                .collect(Collectors.toMap(k -> (String) k.get(STYLE_CODE_FIELD),
                        v -> v.get(INVENTORY_FIELD) == null ? BigDecimal.ZERO : (BigDecimal) v.get(INVENTORY_FIELD)));


        // 过滤出只有库存的数据
        List<String> onlyInventory;
        Map<String, String> onlyInventoryMap;
        List<String> businessIds1 = dataList.stream().map(d -> (String) d.get(STYLE_CODE_FIELD)).collect(Collectors.toList());
        List<String> businessIds2 = dataEndMonth.stream().map(d -> (String) d.get(STYLE_CODE_FIELD)).collect(Collectors.toList());
        onlyInventory = businessIds2.stream().filter(item -> !businessIds1.contains(item)).collect(Collectors.toList());
        onlyInventoryMap = dataEndMonth.stream()
                .filter(item -> onlyInventory.contains((String)item.get(STYLE_CODE_FIELD)))
                .collect(Collectors.toMap(k -> (String) k.get(STYLE_CODE_FIELD),
                        v -> v.get(STYLE_NAME_FIELD) == null ? "空" : (String) v.get(STYLE_NAME_FIELD)));

        // 排序按正价销量+特价销量之合高>低
        dataList.sort((o1, o2) -> (o2.get(POSITIVE_SALES_VOLUME_FIELD_NAME) == null ? BigDecimal.ZERO : (BigDecimal)o2.get(POSITIVE_SALES_VOLUME_FIELD_NAME))
                .add(o2.get(SPECIAL_SALES_VOLUME_FIELD_NAME) == null ? BigDecimal.ZERO : (BigDecimal)o2.get(SPECIAL_SALES_VOLUME_FIELD_NAME))
                .compareTo((o1.get(POSITIVE_SALES_VOLUME_FIELD_NAME) == null ? BigDecimal.ZERO : (BigDecimal) o1.get(POSITIVE_SALES_VOLUME_FIELD_NAME))
                        .add(o1.get(SPECIAL_SALES_VOLUME_FIELD_NAME) == null ? BigDecimal.ZERO : (BigDecimal) o1.get(SPECIAL_SALES_VOLUME_FIELD_NAME))));

        BigDecimal qtySum = BigDecimal.ZERO;
        BigDecimal positiveSalesVolumeSum = BigDecimal.ZERO;
        BigDecimal specialSalesVolumeSum = BigDecimal.ZERO;
        BigDecimal inventorySum = BigDecimal.ZERO;
        for (Map map : dataList) {
            BigDecimal positiveSalesVolume = map.get(POSITIVE_SALES_VOLUME_FIELD_NAME) == null ? BigDecimal.ZERO : (BigDecimal) map.get(POSITIVE_SALES_VOLUME_FIELD_NAME);
            BigDecimal specialSalesVolume = map.get(SPECIAL_SALES_VOLUME_FIELD_NAME) == null ? BigDecimal.ZERO : (BigDecimal) map.get(SPECIAL_SALES_VOLUME_FIELD_NAME);
            BigDecimal qty = positiveSalesVolume.add(specialSalesVolume);
            qtySum = qtySum.add(qty);
            positiveSalesVolumeSum = positiveSalesVolumeSum.add(positiveSalesVolume);
            specialSalesVolumeSum = specialSalesVolumeSum.add(specialSalesVolume);
            BigDecimal endInventory = endInventoryMap.getOrDefault((String) map.get(STYLE_CODE_FIELD), BigDecimal.ZERO);
            inventorySum = inventorySum.add(endInventory);
            BigDecimal digestibility = endInventory.add(qty).compareTo(BigDecimal.ZERO) != 0 ? qty.divide(endInventory.add(qty), 4, RoundingMode.HALF_UP) : BigDecimal.ONE;


            List<Object> row = new ArrayList<>();
            row.add(map.get(STYLE_NAME_FIELD) == null ? map.get(STYLE_CODE_FIELD) : map.get(STYLE_NAME_FIELD));
            row.add(positiveSalesVolume);
            row.add(specialSalesVolume);
            row.add(endInventory);
            row.add(digestibility.multiply(new BigDecimal(100)));
            row.add(map.get(STYLE_CODE_FIELD));
            datas.add(row);
        }

        //添加只有库存的数据
        if(!onlyInventory.isEmpty() && !onlyInventoryMap.isEmpty()){
            for(String item : onlyInventory) {
                List<Object> row = new ArrayList<>();
                row.add(onlyInventoryMap.get(item));
                row.add(BigDecimal.ZERO);
                row.add(BigDecimal.ZERO);
                row.add(endInventoryMap.get(item));
                inventorySum = inventorySum.add(endInventoryMap.get(item));
                row.add(BigDecimal.ZERO); 
                row.add(item);
                datas.add(row);
            }
        }


        //暂不处理
        List<List<Object>> total = new ArrayList<>();
        total.add(Arrays.asList(POSITIVE_SALES_VOLUME_INDEX_NAME, positiveSalesVolumeSum, null, 0));
        total.add(Arrays.asList(SPECIAL_SALES_VOLUME_INDEX_NAME, specialSalesVolumeSum, null, 0));
        if(qtySum.doubleValue() + inventorySum.doubleValue() > 0){
            total.add(Arrays.asList(DIGESTIBILITY_INDEX_NAME, qtySum.multiply(BigDecimal.valueOf(100)).divide(qtySum.add(inventorySum), 2, RoundingMode.HALF_UP) + "%", null, 1));
        }else {
            total.add(Arrays.asList(DIGESTIBILITY_INDEX_NAME, "100.00%", null, 1));
        }

        // 重名的加上编码
        Set<String> nameSet = new HashSet<>();
        datas.forEach(row -> {
            if (nameSet.contains(row.get(0))) {
                row.set(0, row.get(0) + "(" + row.get(5) + ")");
            } else {
                nameSet.add((String) row.getFirst());
            }
        });

        chart.setDatas(datas);
//        chart.setExtend(total);
        return chart;
    }

    public DatatablePage threeSevenTable(TableReportQry reportQry) {
        String seasonParam = reportQry.getParams().stream().filter(item -> ParamType.SEASON.getValue().equals(item.getType())).findFirst().map(ParamQry::getCode).orElse(null);
        // 款式过滤
        List<String> codes = new ArrayList<>();
        if (StringUtil.isNotEmpty(seasonParam)) {
            Response<List<ProductStyleQueryVO>> res = productStyleRemoteService.selectList(new ProductStylePageQry().setSeason(seasonParam));
            if (!res.isSuccess()) {
                return new DatatablePage();
            }
            codes.addAll(res.get().stream().map(ProductStyleQueryVO::getCode).collect(Collectors.toList()));
        }

        DatatablePage dimensionTableData = new DatatablePage();
        List<Title> titleList = new ArrayList<>();
        titleList.add(new Title().setName("名称").setCode(STYLE_NAME_FIELD));
        titleList.add(new Title().setName("款式编码").setCode("code"));
        titleList.add(new Title().setName(POSITIVE_SALES_VOLUME_INDEX_NAME).setCode(POSITIVE_SALES_VOLUME_FIELD_NAME));
        titleList.add(new Title().setName(SPECIAL_SALES_VOLUME_INDEX_NAME).setCode(SPECIAL_SALES_VOLUME_FIELD_NAME));
        titleList.add(new Title().setName("库存").setCode(INVENTORY_FIELD));
        titleList.add(new Title().setName("售罄率").setCode(DIGESTIBILITY_FIELD_NAME));
//        titleList.add(new Title().setName("销量占比").setCode("salesShare"));

        dimensionTableData.setTitles(titleList);
        dimensionTableData.setSize(reportQry.getPageSize());
        dimensionTableData.setCurrent(reportQry.getPageNum());


        // 售罄率 = 销量 / (最后一个月的期末库存 + 销量)
        // 累计销售占比 = 单款销量/总销量 （每个款式顺序累加）
        List<GroupField> groupFields = new ArrayList<>(Arrays.asList(
//                new GroupField().setFieldName(QTY_FIELD_NAME).setGroupType(GroupType.SUM.getValue()),
                new GroupField().setFieldName(POSITIVE_SALES_VOLUME_FIELD_NAME).setGroupType(SUM.getValue()),
                new GroupField().setFieldName(SPECIAL_SALES_VOLUME_FIELD_NAME).setGroupType(SUM.getValue())));

        //设置周度查询时的日期格式
        if(DateType.WEEK.equals(reportQry.getDateType())){
            reportQry.setStartDate(DateUtils.getWeek(reportQry.getStartDate()));
            reportQry.setEndDate(DateUtils.getWeek(reportQry.getEndDate()));
        }

        // 合计查询
        DimensionDataGroupQry qry = new DimensionDataGroupQry()
                .setSalesType(reportQry.getSalesType())
                .setDeptId(reportQry.getDeptId())
                .setDateType(reportQry.getDateType().getValue())
                .setStartDate(reportQry.getStartDate())
                .setEndDate(reportQry.getEndDate())
                .setGroupFields(groupFields)
                .setGroupBy(Arrays.asList(STYLE_CODE_FIELD, STYLE_NAME_FIELD));

        if (!codes.isEmpty()) {
            Map<String, Object> other = new HashMap<>();
            other.put(STYLE_CODE_FIELD, codes);
            qry.setOtherConditions(other);
        }


        List<Map> dataList;
        List<Map> dataEndMonth;
        if (StringUtil.isNotEmpty(seasonParam) && codes.isEmpty()) {
            dataList = new ArrayList<>();
            dataEndMonth = new ArrayList<>();
        } else {
            // 先查询原数据
            String sql = SqlUtils.buildSql(qry, "ads_base_product_multi_sales");
            dataList = jdbcService.selectList(sql, new Object[]{});

            // 最后一个月期末库存
            String s = productDataQueryExe.selectMaxInventoryDate(reportQry.getStartDate(), reportQry.getEndDate(), reportQry.getDateType().getValue());
            if(TYPE_WEEK.equals(reportQry.getDateType().getValue())){
                qry.setStartDate(DateUtils.getWeek(s))
                        .setEndDate(DateUtils.getWeek(s));
            }else {
                qry.setStartDate(s)
                        .setEndDate(s);
            }
            dataEndMonth = productDataQueryExe.selectInventoryData(qry, STYLE_CODE_FIELD, new ArrayList<>());
        }

        // 重复编码处理特殊处理
        Map<String, Map> dataEndMonthMap = new HashMap<>();
        for (Map map : dataEndMonth) {
            String busId = (String) map.get(STYLE_CODE_FIELD);
            if (!dataEndMonthMap.containsKey(busId)) {
                dataEndMonthMap.put(busId, map);
            } else {
                Map oldData = dataEndMonthMap.get(busId);
                oldData.put(INVENTORY_FIELD, (oldData.get(INVENTORY_FIELD) == null ? BigDecimal.ZERO : (BigDecimal) oldData.get(INVENTORY_FIELD))
                        .add(map.get(INVENTORY_FIELD) == null ? BigDecimal.ZERO : (BigDecimal) map.get(INVENTORY_FIELD)));
            }
        }
        dataEndMonth = new ArrayList<>(dataEndMonthMap.values());

        Map<String, BigDecimal> endInventoryMap = dataEndMonth.stream()
                .collect(Collectors.toMap(k -> (String) k.get(STYLE_CODE_FIELD),
                        v -> v.get(INVENTORY_FIELD) == null ? BigDecimal.ZERO : (BigDecimal) v.get(INVENTORY_FIELD)));

        List<String> onlyInventory;
        Map<String, String> onlyInventoryMap;
        List<String> businessIds1 = dataList.stream().map(d -> (String) d.get(STYLE_CODE_FIELD)).collect(Collectors.toList());
        List<String> businessIds2 = dataEndMonth.stream().map(d -> (String) d.get(STYLE_CODE_FIELD)).collect(Collectors.toList());
        onlyInventory = businessIds2.stream().filter(item -> !businessIds1.contains(item)).collect(Collectors.toList());
        onlyInventoryMap = dataEndMonth.stream()
                .filter(item -> onlyInventory.contains((String)item.get(STYLE_CODE_FIELD)))
                .collect(Collectors.toMap(k -> (String) k.get(STYLE_CODE_FIELD),
                        v -> v.get(STYLE_NAME_FIELD) == null ? "空" : (String) v.get(STYLE_NAME_FIELD)));

        // 总售量
//        BigDecimal qrySum = dataList.stream()
//                .map(d -> (BigDecimal) d.get(QTY_FIELD_NAME))
//                .filter(Objects::nonNull)
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//        // 累计销量百分百比
//        BigDecimal qryPresent = BigDecimal.ZERO;

        // 排序按正价销量+特价销量之合高>低
        dataList.sort((o1, o2) -> ((BigDecimal)o2.get(POSITIVE_SALES_VOLUME_FIELD_NAME)).add((BigDecimal)o2.get(SPECIAL_SALES_VOLUME_FIELD_NAME))
                .compareTo(((BigDecimal) o1.get(POSITIVE_SALES_VOLUME_FIELD_NAME)).add((BigDecimal) o1.get(SPECIAL_SALES_VOLUME_FIELD_NAME))));
        // 合计
        BigDecimal positiveSalesVolumeSum = BigDecimal.ZERO;
        BigDecimal specialSalesVolumeSum = BigDecimal.ZERO;
        BigDecimal inventorySum = BigDecimal.ZERO;
        BigDecimal qtySum = BigDecimal.ZERO;

        // 列表内容
        List<Map<String , String>> datas = new ArrayList<>();
        for (Map map : dataList) {
//            BigDecimal qty = map.get(QTY_FIELD_NAME) == null ? BigDecimal.ZERO : (BigDecimal) map.get(QTY_FIELD_NAME);
//            if (qty.compareTo(BigDecimal.ZERO) != 0) {
//                qryPresent = qryPresent.add(qty.divide(qrySum, 7, RoundingMode.HALF_UP));
//
//            }
            BigDecimal positiveSalesVolume = map.get(POSITIVE_SALES_VOLUME_FIELD_NAME) == null ? BigDecimal.ZERO : (BigDecimal) map.get(POSITIVE_SALES_VOLUME_FIELD_NAME);
            BigDecimal specialSalesVolume = map.get(SPECIAL_SALES_VOLUME_FIELD_NAME) == null ? BigDecimal.ZERO : (BigDecimal) map.get(SPECIAL_SALES_VOLUME_FIELD_NAME);
            BigDecimal qty = positiveSalesVolume.add(specialSalesVolume);
            BigDecimal endInventory = endInventoryMap.getOrDefault((String) map.get(STYLE_CODE_FIELD), BigDecimal.ZERO);
            BigDecimal digestibility = endInventory.add(qty).compareTo(BigDecimal.ZERO) != 0 ? qty.divide(endInventory.add(qty), 4, RoundingMode.HALF_UP) : BigDecimal.ONE;


            Map<String, String> row = new LinkedHashMap<>();
            row.put(STYLE_NAME_FIELD, map.get(STYLE_NAME_FIELD) == null ? map.get(STYLE_CODE_FIELD).toString() : map.get(STYLE_NAME_FIELD).toString());
            row.put("code", map.get(STYLE_CODE_FIELD).toString());
            row.put(POSITIVE_SALES_VOLUME_FIELD_NAME, positiveSalesVolume.setScale(0, RoundingMode.HALF_UP).toPlainString());
            row.put(SPECIAL_SALES_VOLUME_FIELD_NAME, specialSalesVolume.setScale(0, RoundingMode.HALF_UP).toPlainString());
            row.put(INVENTORY_FIELD, endInventory.setScale(0, RoundingMode.HALF_UP).toPlainString());
            row.put(DIGESTIBILITY_FIELD_NAME, digestibility.multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toPlainString()+"%");
//            row.put("salesShare", qryPresent.multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toPlainString());
//            System.out.println("款式:" + map.get(STYLE_NAME_FIELD) + "   销量： " + qty +  "    百分百：" + qryPresent);
            datas.add(row);

            positiveSalesVolumeSum = positiveSalesVolumeSum.add(positiveSalesVolume);
            specialSalesVolumeSum = specialSalesVolumeSum.add(specialSalesVolume);
            inventorySum = inventorySum.add(endInventory);
            qtySum = qtySum.add(qty);
        }


        //添加只有库存的数据
        if(!onlyInventory.isEmpty() && !onlyInventoryMap.isEmpty()){
            for (String item : onlyInventory){
                Map<String, String> row = new LinkedHashMap<>();
                row.put(STYLE_NAME_FIELD, onlyInventoryMap.get(item));
                row.put(POSITIVE_SALES_VOLUME_FIELD_NAME, "0");
                row.put(SPECIAL_SALES_VOLUME_FIELD_NAME, "0");
                row.put(INVENTORY_FIELD, endInventoryMap.get(item) + "");
                row.put(DIGESTIBILITY_FIELD_NAME, "0%");
//            row.put("salesShare", qryPresent.multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toPlainString());
//            System.out.println("款式:" + map.get(STYLE_NAME_FIELD) + "   销量： " + qty +  "    百分百：" + qryPresent);
                datas.add(row);

                inventorySum = inventorySum.add(endInventoryMap.get(item));
            }
        }


        // 合计
        BigDecimal digestibilitySum = inventorySum.add(qtySum).compareTo(BigDecimal.ZERO) != 0 ? qtySum.divide(inventorySum.add(qtySum), 4, RoundingMode.HALF_UP) : BigDecimal.ZERO;
        Map<String, String> totalRow = new LinkedHashMap<>();
        totalRow.put(STYLE_NAME_FIELD, "合计");
        totalRow.put(POSITIVE_SALES_VOLUME_FIELD_NAME, positiveSalesVolumeSum.setScale(0, RoundingMode.HALF_UP).toPlainString());
        totalRow.put(SPECIAL_SALES_VOLUME_FIELD_NAME, specialSalesVolumeSum.setScale(0, RoundingMode.HALF_UP).toPlainString());
        totalRow.put(INVENTORY_FIELD, inventorySum.setScale(0, RoundingMode.HALF_UP).toPlainString());
        totalRow.put(DIGESTIBILITY_FIELD_NAME, digestibilitySum.multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toPlainString()+"%");
//        totalRow.put("salesShare", "100.00");
        datas.add(0, totalRow);
        dimensionTableData.setTotal(datas.size());

        //设置周度计划所需汇总值
//        List<ValueVO> sum = new ArrayList<>();
//        List<ValueVO> avg = new ArrayList<>();
//        sum.add(new ValueVO().setName(POSITIVE_SALES_VOLUME_INDEX_NAME).setValue(positiveSalesVolumeSum.setScale(0, RoundingMode.HALF_UP).toPlainString()));
//        sum.add(new ValueVO().setName(SPECIAL_SALES_VOLUME_INDEX_NAME).setValue(specialSalesVolumeSum.setScale(0, RoundingMode.HALF_UP).toPlainString()));
//        sum.add(new ValueVO().setName(INVENTORY_INDEX_NAME).setValue(inventorySum.setScale(0, RoundingMode.HALF_UP).toPlainString()));
//        avg.add(new ValueVO().setName(DIGESTIBILITY_INDEX_NAME).setValue(digestibilitySum.multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toPlainString()+"%"));
//        dimensionTableData.setSumData(sum);
//        dimensionTableData.setAvgData(sum);


        if (reportQry.getOffset() < datas.size()) {
            dimensionTableData.setDatas(datas.subList(reportQry.getOffset(), Math.min(reportQry.getOffset() + reportQry.getPageSize(), datas.size())));
        } else {
            dimensionTableData.setDatas(new ArrayList<>());
        }
        return dimensionTableData;
    }

}
